<template>
  <view class="signature" :class="{ landscape }">
    <text class="signature-text">请在此处签名</text>
    <view class="signature-area">
      <l-signature
        disableScroll
        ref="signatureRef"
        :penColor="penColor"
        :penSize="penSize"
        :key="landscape"
        uid="0"
        :minLineWidth="2"
        :openSmooth="openSmooth"
        :boundingBox="bBox"
        landscape
      ></l-signature>
    </view>
    <view class="tools">
      <view class="tools-item" @tap="isShowPen = !isShowPen">
        <!-- #ifdef APP-NVUE -->
        <text>设置</text>
        <!-- #endif -->
        <!-- #ifndef APP-NVUE -->
        <text class="fu-iconfont2">&#xe711;</text>
        <!-- #endif -->
      </view>
      <view class="tools-item" @tap="onClick('undo')">
        <!-- #ifdef APP-NVUE -->
        <text>撤消</text>
        <!-- #endif -->
        <!-- #ifndef APP-NVUE -->
        <text class="fu-iconfont2">&#xe62d;</text>
        <!-- #endif -->
      </view>
      <view class="tools-item restore" @tap="onClick('restore')">
        <!-- #ifdef APP-NVUE -->
        <text>上一步</text>
        <!-- #endif -->
        <!-- #ifndef APP-NVUE -->
        <text class="fu-iconfont2">&#xe62d;</text>
        <!-- #endif -->
      </view>
      <view class="tools-item" @tap="onClick('clear')">
        <!-- #ifdef APP-NVUE -->
        <text>清空</text>
        <!-- #endif -->
        <!-- #ifndef APP-NVUE -->
        <text class="fu-iconfont2">&#xe7c8;</text>
        <!-- #endif -->
      </view>
      <view class="tools-item">
        <button type="default" size="mini" @tap="onCancel" style="border: 1px solid #ccc; background: #fff">取消</button>
      </view>
      <view class="tools-item"><button type="primary" size="mini" @tap="onClick('save')" class="margin-left-xs">保存</button></view>
    </view>
    <uni-popup ref="popup" type="bottom" @change="isShowPen = $event.show">
      <view class="popup-wrap">
        <view class="popup-item">
          <view class="flex align-center padding-tb-sm">
            <view>模拟压感</view>
            <view class="flex-sub text-right padding-right">
              <text>{{ openSmooth ? '开' : '关' }}</text>
              <switch class="popup-item__more-switch" @change="() => (openSmooth = !openSmooth)" :checked="openSmooth" />
            </view>
          </view>
          <view class="flex align-center padding-top-sm">
            <view>画笔粗细</view>
            <view class="flex-sub">
              <slider :value="penSize" max="20" @change="penSize = $event.detail.value" show-value />
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import { base64ToPath } from '@/common/utils/utils.js'
import lSignature from './l-signature/l-signature.vue'
export default {
  components: {
    lSignature,
  },
  data() {
    return {
      penColor: 'red',
      penSize: 8,
      aurl: '',
      show: false,
      landscape: true,
      openSmooth: true,
      bottomHeight: 0,
      bBox: true,
      isShowPen: false,
      customBar: this.CustomBar || 0,
    }
  },
  watch: {
    isShowPen(v) {
      if (!v) {
        this.$refs.popup.close()
      } else {
        this.$refs.popup.open()
      }
    },
  },
  methods: {
    open() {
      // 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
      this.$refs.popup.open()
      this.show = true
    },
    onCancel() {
      uni.navigateBack()
    },
    onClick(type) {
      const { signatureRef } = this.$refs
      if (type == 'openSmooth') {
        this.openSmooth = !this.openSmooth
        return
      }
      if (type == 'save') {
        signatureRef.canvasToTempFilePath({
          // destWidth: 300,
          // destHeight: 200,
          quality: 0.8,
          fileType: 'png',
          success: async (res) => {
            let filepath = ''
            // #ifdef H5
            filepath = await base64ToPath(res.tempFilePath)
            // #endif

            // #ifndef H5
            filepath = res.tempFilePath
            // #endif
            if (!res.isEmpty) {
              uni.getImageInfo({
                src: filepath,
                success(res) {
                  uni.$emit('getSignImg', {
                    base64: res.path,
                  })
                  uni.navigateBack({
                    delta: 1,
                  })
                },
              })
            } else {
              uni.showToast({
                title: '请签名',
                icon: 'none',
              })
            }
          },
        })
        return
      }
      if (signatureRef) signatureRef[type]()
    },
  },
}
</script>

<style lang="scss">
.signature {
  /* #ifndef APP-NVUE */
  height: calc(100vh - var(--window-top));
  /* #endif */
  /* #ifdef APP-NVUE */
  flex: 1;

  /* #endif */
  &-area {
    /* #ifndef APP-NVUE */
    height: calc(100vh - var(--window-top) - 120rpx);
    /* #endif */
    /* #ifdef APP-NVUE */
    flex: 1;
    /* #endif */
  }

  &-text {
    position: absolute;
    /* #ifndef APP-NVUE */
    left: 50%;
    top: 50%;
    /* #endif */
    /* #ifdef APP-NVUE */
    left: 375rpx;
    top: 375rpx;
    /* #endif */
    font-size: 60rpx;
    color: #ddd;
    transform: translate(-50%, -50%);
    z-index: -1;
    transition: transform 300ms;
  }

  &-preview {
    position: absolute;
    bottom: 120rpx;
    right: 20rpx;
    width: 120px;
    background: #000;
    box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.05);

    &__image {
      /* #ifndef APP-NVUE */
      width: 100%;
      /* #endif */
      /* #ifdef APP-NVUE */
      flex: 1;
      /* #endif */
    }
  }

  &.landscape {
    .signature-preview {
      bottom: 20rpx;
      width: 220px;
    }

    .signature-text {
      transform: translate(-50%, -50%) rotate(90deg);
    }

    .tools {
      z-index: 1;
      position: absolute;
      left: 20rpx;
      bottom: 10rpx;
      transform: rotate(90deg) translate(-100%, 0%);
      transform-origin: bottom left;
      padding-left: 16rpx;
      border-top-left-radius: 50rpx;
      border-bottom-left-radius: 50rpx;
      margin-bottom: 30rpx;

      &_item {
        padding: 0 16rpx;
      }
    }

    // .popup-wrap {
    // 	box-sizing: border-box;
    // 	height: 750rpx;
    // 	padding-top: 100px;
    // 	padding-bottom: 0;
    // 	transform: rotate(90deg) translate(-100%, 0%);
    // 	transform-origin: bottom left;
    // }
  }
}

.screen-handle {
  position: absolute;
  right: 10rpx;
  top: 100rpx;
  width: 60rpx;
  height: 60rpx;
  background-color: white;
  border-radius: 10rpx;
  border: 1rpx solid #ddd;
  z-index: 10;
  justify-content: center;
  align-items: center;

  &__image {
    /* #ifndef APP-NVUE */
    width: 80%;
    /* #endif */
    /* #ifdef APP-NVUE */
    flex: 0.8;
    /* #endif */
  }
}

.tools {
  background-color: #fff;
  position: relative;
  z-index: 999999;
  display: flex;
  height: 120rpx;
  box-shadow: 0 -10rpx 30rpx rgba(0, 0, 0, 0.05);
  /* #ifdef APP-NVUE */
  flex-direction: row;
  /* #endif */
  /* #ifndef APP-NVUE */
  padding-bottom: constant(safe-area-inset-bottom);
  margin-bottom: env(safe-area-inset-bottom);

  /* #endif */
  &-item {
    flex: 1;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    align-items: center;
    justify-content: center;
    opacity: 0.6;
    transition: opacity 0.1s ease-out;

    &.restore {
      /* #ifndef APP-NVUE */
      transform: scaleX(-1);
      /* #endif */
    }

    &.active {
      opacity: 1;
    }

    &__image {
      width: 60rpx;
    }
  }
}

.popup {
  &-wrap {
    padding-bottom: 120rpx;
    background-color: #fff;
  }

  &-item {
    padding-bottom: 30rpx;

    &__head {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      padding: 30rpx;
    }

    &__title {
      font-size: 30rpx;
    }

    &__more {
      flex-direction: row;

      &-text {
        font-size: 24rpx;
        // padding-right 10rpx
        color: #999;
      }

      &-switch {
        transform: scale(0.7);
        transform-origin: center right;
      }
    }
  }
}
</style>
